package com.fendou.virtualphone.activity;

import com.fendou.virtualphone.R;
import com.fendou.virtualphone.bean.CallBeanInfo;
import com.fendou.virtualphone.db.CallPhoneHistoryDao;
import com.fendou.virtualphone.impl.CallPhoneHistoryImpl;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private CallPhoneHistoryDao callPhoneHistoryDao;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);    
		
		callPhoneHistoryDao = new CallPhoneHistoryImpl(this);
		
		startService( new Intent(this,PhoneIncomingActivity.class));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void onMainClick(View v){
		
		switch (v.getId()) {
		case R.id.add_contact:

			final Dialog dialog = new Dialog(this, R.style.mydialog);
			dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
			dialog.setContentView(R.layout.contact_layout);
			dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			//LayoutParams lay = dialog.getWindow().getAttributes();
			//setParams(lay);
			dialog.show();
			
			Button submitBtn = (Button) dialog.findViewById(R.id.contact_submit);
			submitBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					//保存设置的数据
					callPhoneHistoryDao.saveCallBean("", initCallRecond(dialog) );
					dialog.cancel();
				}
			});
				
			
			Button cancalBtn = (Button) dialog.findViewById(R.id.contact_cancel);
			
			cancalBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					dialog.cancel();
				}
			});
			
			break;
		case R.id.history:
			Toast.makeText(this, "hahhah", 3000).show();
			
			break;

		default:
			break;
		}
		
		
		
		
	}
	private void setParams(LayoutParams lay) {
		  DisplayMetrics dm = new DisplayMetrics();
		  getWindowManager().getDefaultDisplay().getMetrics(dm);
		  Rect rect = new Rect();
		  View view = getWindow().getDecorView();
		  view.getWindowVisibleDisplayFrame(rect);
		  lay.height = dm.heightPixels - rect.top;
		  lay.width = dm.widthPixels;
		 }
	
	private CallBeanInfo initCallRecond(Dialog dialog){
		
		String name =((EditText) dialog.findViewById(R.id.name)).getText().toString();
		String phone_num =((EditText) dialog.findViewById(R.id.phone_num)).getText().toString();
		String call_time =((DatePicker) dialog.findViewById(R.id.call_time)).getDayOfMonth()+"";   
		String ring =((CheckBox) dialog.findViewById(R.id.shakeAndRing)).getText().toString();
		
		CallBeanInfo callBean = new CallBeanInfo();
		callBean.setName(name);
		callBean.setPhone_num(phone_num);
		callBean.setCall_time(call_time);
		
		callBean.setRing_mode(ring);
		
		callBean.setCurrent_call("0");
		callBean.setCall_type("0");
		
		return callBean;
		
	}

}
